/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.app.gui;

import Facebook.ShareStatut;
import Facebook.ShareVideo;
import Facebook.Sharingimages;
import charts.Connexion;
import edu.app.dao.RecetteDAO;
import edu.app.dao.IDAO;
import edu.app.persistence.Recette;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.sql.SQLException;
import java.util.List;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import org.jdesktop.observablecollections.ObservableCollections;
import wshop.mail.MailJframe;

/**
 *
 * @author Karim
 */

public class Form_All_Recipes extends javax.swing.JFrame {
    
   
    private IDAO<Recette> recetteDAO= new RecetteDAO();

    /**
     * Creates new form RecetteMasterDetailsForm
     */
    public Form_All_Recipes() {
        initComponents();
           setResizable(false);
        setBounds(50, 50, 1100, 830);
        getContentPane().setLayout(null);
       Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        Dimension windowSize = this.getSize();
        int windowX = Math.max(0, (screenSize.width  - windowSize.width ) / 2);
        int windowY = Math.max(0, (screenSize.height - windowSize.height) / 2)-20;
        this.setLocation(windowX, windowY);
       TableCellRenderer rendererFromHeader = masterTable.getTableHeader().getDefaultRenderer();
JLabel headerLabel = (JLabel) rendererFromHeader;
headerLabel.setHorizontalAlignment(JLabel.CENTER);
DefaultTableCellRenderer dtcr = new DefaultTableCellRenderer();
dtcr.setHorizontalAlignment(SwingConstants.CENTER);
masterTable.getColumn("Cout").setCellRenderer(dtcr);
masterTable.getColumn("Duree").setCellRenderer(dtcr);
masterTable.getColumn("Nom").setCellRenderer(dtcr);
masterTable.getColumn("Origine").setCellRenderer(dtcr);
       
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        recettes = ObservableCollections.observableList(recetteDAO.findAll(""));
        scrolllPane = new javax.swing.JScrollPane();
        masterTable = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        imagerecette2 = new javax.swing.JLabel();
        jDesktopPane4 = new javax.swing.JDesktopPane();
        imagerecette4 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel16 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu3 = new javax.swing.JMenu();
        jMenuItem6 = new javax.swing.JMenuItem();
        jMenu4 = new javax.swing.JMenu();
        jMenuItem4 = new javax.swing.JMenuItem();
        jMenuItem5 = new javax.swing.JMenuItem();

        setTitle("Toutes les recettes");
        getContentPane().setLayout(null);

        masterTable.setFont(new java.awt.Font("Gentium Book Basic", 3, 18));
        masterTable.setRowHeight(30);

        org.jdesktop.swingbinding.JTableBinding jTableBinding = org.jdesktop.swingbinding.SwingBindings.createJTableBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, recettes, masterTable);
        org.jdesktop.swingbinding.JTableBinding.ColumnBinding columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${nom}"));
        columnBinding.setColumnName("Nom");
        columnBinding.setColumnClass(String.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${cout}"));
        columnBinding.setColumnName("Cout");
        columnBinding.setColumnClass(Float.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${duree}"));
        columnBinding.setColumnName("Duree");
        columnBinding.setColumnClass(Integer.class);
        columnBinding = jTableBinding.addColumnBinding(org.jdesktop.beansbinding.ELProperty.create("${origine}"));
        columnBinding.setColumnName("Origine");
        columnBinding.setColumnClass(String.class);
        bindingGroup.addBinding(jTableBinding);
        jTableBinding.bind();
        masterTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                masterTableMouseClicked(evt);
            }
        });
        scrolllPane.setViewportView(masterTable);

        getContentPane().add(scrolllPane);
        scrolllPane.setBounds(10, 220, 660, 540);

        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/home.jpg"))); // NOI18N
        jLabel2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel2MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel2MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel2MouseExited(evt);
            }
        });
        getContentPane().add(jLabel2);
        jLabel2.setBounds(170, 20, 70, 70);
        getContentPane().add(imagerecette2);
        imagerecette2.setBounds(740, 130, 320, 200);

        imagerecette4.setBounds(-60, 40, 300, 200);
        jDesktopPane4.add(imagerecette4, javax.swing.JLayeredPane.DEFAULT_LAYER);

        getContentPane().add(jDesktopPane4);
        jDesktopPane4.setBounds(780, 150, 240, 160);

        jLabel3.setFont(new java.awt.Font("Gabriola", 0, 18));
        jLabel3.setText("Precedent");
        getContentPane().add(jLabel3);
        jLabel3.setBounds(50, 80, 80, 31);

        jLabel13.setFont(new java.awt.Font("Gabriola", 0, 18));
        jLabel13.setText("Acceuil");
        getContentPane().add(jLabel13);
        jLabel13.setBounds(180, 90, 60, 14);

        jLabel9.setFont(new java.awt.Font("Gabriola", 2, 30));
        jLabel9.setText("Trier Par :");
        getContentPane().add(jLabel9);
        jLabel9.setBounds(10, 150, 110, 40);

        jComboBox1.setFont(new java.awt.Font("Gabriola", 3, 18));
        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nom", "Duree", "Cout", "Favories en premier" }));
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });
        getContentPane().add(jComboBox1);
        jComboBox1.setBounds(130, 160, 170, 37);

        jLabel16.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/ret1.jpg"))); // NOI18N
        jLabel16.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel16MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel16MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel16MouseExited(evt);
            }
        });
        getContentPane().add(jLabel16);
        jLabel16.setBounds(40, 30, 90, 50);

        jLabel6.setFont(new java.awt.Font("Nadall", 2, 30));
        jLabel6.setText("Detailles de la recette");
        jLabel6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jLabel6MouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                jLabel6MouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                jLabel6MouseExited(evt);
            }
        });
        getContentPane().add(jLabel6);
        jLabel6.setBounds(780, 350, 270, 30);

        jLabel1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/All_recips.jpg"))); // NOI18N
        getContentPane().add(jLabel1);
        jLabel1.setBounds(0, 0, 1120, 840);

        jMenu1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/stats.jpg"))); // NOI18N
        jMenu1.setText("Statistiques");
        jMenuBar1.add(jMenu1);

        jMenu2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/mailpetit.jpg"))); // NOI18N
        jMenu2.setText("Contact");
        jMenu2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenu2ActionPerformed(evt);
            }
        });

        jMenuItem1.setText("Envoyer Un Mail");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem1);

        jMenuBar1.add(jMenu2);

        jMenu3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/fbpetit.jpg"))); // NOI18N
        jMenu3.setText("Facebook");

        jMenuItem6.setText("Partager un statut");
        jMenuItem6.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenuItem6MouseClicked(evt);
            }
        });
        jMenuItem6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem6ActionPerformed(evt);
            }
        });
        jMenu3.add(jMenuItem6);

        jMenu4.setText("Partager media");

        jMenuItem4.setText("Image");
        jMenuItem4.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenuItem4MouseClicked(evt);
            }
        });
        jMenuItem4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem4ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem4);

        jMenuItem5.setText("Video");
        jMenuItem5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jMenuItem5MouseClicked(evt);
            }
        });
        jMenuItem5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem5ActionPerformed(evt);
            }
        });
        jMenu4.add(jMenuItem5);

        jMenu3.add(jMenu4);

        jMenuBar1.add(jMenu3);

        setJMenuBar(jMenuBar1);

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jLabel6MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel6MouseEntered
         jLabel6.setForeground(Color.BLUE);
    }//GEN-LAST:event_jLabel6MouseEntered

    private void jLabel6MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel6MouseExited
       jLabel6.setForeground(Color.BLACK);
    }//GEN-LAST:event_jLabel6MouseExited

    private void jLabel2MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel2MouseEntered
       jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/home2.jpg")));
    }//GEN-LAST:event_jLabel2MouseEntered

    private void jLabel2MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel2MouseExited
        jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/home.jpg")));
    }//GEN-LAST:event_jLabel2MouseExited

    private void jLabel6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel6MouseClicked
      if (masterTable.getSelectedRow()!=-1){
      
         String Titre= masterTable.getValueAt(masterTable.getSelectedRow(),0).toString();

       /*String categ= masterTable.getValueAt(masterTable.getSelectedRow(),1).toString();
       int nbr_per = Integer.parseInt(masterTable.getValueAt(masterTable.getSelectedRow(),2).toString());
       String origine = masterTable.getValueAt(masterTable.getSelectedRow(),3).toString();*/
        //f1.setVisible(true);
        Recette r = new RecetteDAO().find(Titre);
        RecetteIngredients f = new RecetteIngredients(r.getNom(),r.getCategorie(),r.getIngredient(),r.getDuree(),r.getNote(),r.getFavorie(),r.getRemarques(),"null",r.getCout(),r.getNbr_personne(),r.getOrigine());
        f.setVisible(true);}
        else
        {
            JOptionPane.showMessageDialog(this,"Veuillez selectionner une recette", "Aucune recette", JOptionPane.ERROR_MESSAGE);
        }
    }//GEN-LAST:event_jLabel6MouseClicked

    private void jLabel16MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel16MouseClicked

        this.setVisible(false);
          Menu fa = new Menu();
           fa.setVisible(true);
}//GEN-LAST:event_jLabel16MouseClicked

    private void jLabel16MouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel16MouseEntered
        jLabel16.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/ret2.jpg")));
}//GEN-LAST:event_jLabel16MouseEntered

    private void jLabel16MouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel16MouseExited
        jLabel16.setIcon(new javax.swing.ImageIcon(getClass().getResource("/edu/app/gui/images/ret1.jpg")));
}//GEN-LAST:event_jLabel16MouseExited

    private void jLabel2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jLabel2MouseClicked
        Menu fa = new Menu();
           fa.setVisible(true);
           this.setVisible(false);
    }//GEN-LAST:event_jLabel2MouseClicked

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged


        if (jComboBox1.getSelectedItem()=="Nom"){
            List<Recette> loaded = recetteDAO.findAll("order by nom");
            recettes.clear();
            recettes.addAll(loaded);
        } else if (jComboBox1.getSelectedItem()=="Cout"){
            List<Recette> loaded = recetteDAO.findAll("order by cout");
            recettes.clear();
            recettes.addAll(loaded);
        } else if (jComboBox1.getSelectedItem()=="Favories en premier"){
            List<Recette> loaded = recetteDAO.findAll("order by favorie");
            recettes.clear();
            recettes.addAll(loaded);}

        else if  (jComboBox1.getSelectedItem()=="Duree"){
            List<Recette> loaded = recetteDAO.findAll("order by duree");
            recettes.clear();
            recettes.addAll(loaded);
        }
    }//GEN-LAST:event_jComboBox1ItemStateChanged

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
}//GEN-LAST:event_jComboBox1ActionPerformed

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
MailJframe mail2 = new MailJframe();
        mail2.setVisible(true);        // TODO add your handling code here:
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void masterTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_masterTableMouseClicked
       Connexion con = new Connexion();
        int row = masterTable.getSelectedRow();
        String ch = masterTable.getModel().getValueAt(row, 0).toString();
        System.out.println(ch);
        try {
            try {


                Connexion.rs = Connexion.st.executeQuery("select image from recette where nom= '" + ch + "'");
            } catch (SQLException ex) {
            }

            while (Connexion.rs.next()) {
                imagedata = Connexion.rs.getBytes("image");
                ImageIcon format = new ImageIcon(imagedata);
                imagerecette2.setIcon(format);

            }


        } catch (Exception e) {
        }
    }//GEN-LAST:event_masterTableMouseClicked

    private void jMenuItem6MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem6MouseClicked

    }//GEN-LAST:event_jMenuItem6MouseClicked

    private void jMenuItem6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem6ActionPerformed
        ShareStatut shim = new ShareStatut();
        shim.setVisible(true);
}//GEN-LAST:event_jMenuItem6ActionPerformed

    private void jMenuItem4MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem4MouseClicked

}//GEN-LAST:event_jMenuItem4MouseClicked

    private void jMenuItem4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem4ActionPerformed
        Sharingimages shim = new Sharingimages();
        shim.setVisible(true);
}//GEN-LAST:event_jMenuItem4ActionPerformed

    private void jMenuItem5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuItem5MouseClicked

}//GEN-LAST:event_jMenuItem5MouseClicked

    private void jMenuItem5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem5ActionPerformed
        ShareVideo vid = new ShareVideo();
        vid.setVisible(true);
}//GEN-LAST:event_jMenuItem5ActionPerformed

    private void jMenu2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenu2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jMenu2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Windows".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(RecetteMasterDetailsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(RecetteMasterDetailsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(RecetteMasterDetailsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(RecetteMasterDetailsForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Form_All_Recipes().setVisible(true);
            }
        });
    }
    byte []imagedata;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel imagerecette2;
    private javax.swing.JLabel imagerecette4;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JDesktopPane jDesktopPane4;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenu jMenu3;
    private javax.swing.JMenu jMenu4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem4;
    private javax.swing.JMenuItem jMenuItem5;
    private javax.swing.JMenuItem jMenuItem6;
    private javax.swing.JTable masterTable;
    private java.util.List<edu.app.persistence.Recette> recettes;
    private javax.swing.JScrollPane scrolllPane;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
}
